// pages/index/vote/vote.js
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme: {},
    tabs: [{
      type: 1,
      name: "正在投票"
    },{
      type: 0,
      name: "投票结束"
    }],
    list:[],
    state: 1,
    pageNumber: 1,
    pageSize: 10,
    //是否完成刷新
    isRefresh: true,
    isLoading: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "社区投票"
    })
    this.setData({
      theme: app.globalData.theme
    })
    // this.getVoteList()
  },

  /**
   * 获取投票列表
   */
  getVoteList: function () {
    this.setData({
      isLoading: true
    })
    http.get("questions/open/getAppletQuestions",{
      applet_user_id: app.globalData.userInfo.applet_user_id,
      state: this.data.state,
      page_number: this.data.pageNumber,
      page_size: this.data.pageSize,
      area_code:app.globalData.areaCode,
    }).then(res => {
      if (res.data.code === http.REQUEST_OK){
        res.data.data.list.forEach(item => {
          this.data.list.push(item)
        })
        this.setData({
          list: this.data.list,
          isRefresh: false,
          isLoading: false
        })
      } else {
        this.setData({
          isRefresh: false,
          isLoading: false
        })
      }
    }, err => {
      this.setData({
        isRefresh: false,
        isLoading: false
      })
    })
  },

  /**
   * tab切换
   * @param e
   */
  onTabsChange: function (e){
    const index = e.detail.index
    this.data.state = index == 1 ? 0 : 1
    this.data.pageNumber = 1
    this.data.list = []
    this.getVoteList()
  },

  /**
   * 投票详情
   * @param e
   */
  onVoteDetail: function (e) {
    const id = e.currentTarget.dataset.id
    const state = e.currentTarget.dataset.state
    wx.navigateTo({
      url: "./vote_detail/vote_detail?id=" + id + "&state=" + state +"&isEnd=" + this.data.state
    })
  },

  /**
   * 下拉刷新
   * @param e
   */
  onRefresh: function (e) {
    this.data.pageNumber = 1
    this.data.list = []
    this.getVoteList()
  },

  /**
   * 上拉加载更多
   * @param e
   */
  onLower: function (e) {
    this.data.pageNumber += 1
    this.getVoteList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.pageNumber = 1
    this.data.list = []
    this.getVoteList()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})